package com.openspring.platform.model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
//import javax.sql.DataSource;
//import org.springframework.context.ApplicationContext;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.web.context.support.WebApplicationContextUtils;
import com.openspring.framework.util.StringUtils;

public class LoginUser implements HttpSessionBindingListener {

	private Long id;
	private String userName;
	private String loginName;
	
	public LoginUser(){}
	public LoginUser(User user){
		this.id = user.getId();
		this.userName = user.getUserName();
		this.loginName = user.getLoginName();
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Override
	public void valueBound(HttpSessionBindingEvent event) {
//		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getSession().getServletContext());
//		DataSource dataSource = context.getBean("dataSource", DataSource.class);
//		String sql = "INSERT INTO t_login_log(userId, loginName, loginDate, ip, type) VALUES (?, ?, ?, ?, ?)";
//		JdbcTemplate jdbc = new JdbcTemplate(dataSource);
//		jdbc.update(sql, id, loginName, new java.util.Date(), getIpAddress(RequestLocal.getRequest()), 1);
//		Connection conn = null;
//		PreparedStatement prepareStatement = null;
//		try {
//			conn = dataSource.getConnection();
//			conn.setAutoCommit(false);
//			prepareStatement = conn.prepareStatement(sql);
//			prepareStatement.setLong(1, id);
//			prepareStatement.setString(2, loginName);
//			prepareStatement.setDate(3, new Date(new java.util.Date().getTime()));
//			prepareStatement.setString(4, "");
//			prepareStatement.executeUpdate();
//			conn.commit();
//			prepareStatement.close();
//			conn.close();
//		} catch (SQLException e) {
//			try {
//				conn.rollback();
//			} catch (SQLException e1) {
//				throw new SystemException(e1.getMessage());
//			}
//			throw new SystemException(e.getMessage());
//		} finally {
//			try {
//				if(prepareStatement != null) {
//					prepareStatement.close();
//				}
//				if(conn != null) {
//					conn.close();
//				}
//			} catch (SQLException e) {
//				throw new SystemException(e.getMessage());
//			}				
//		}
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
//		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getSession().getServletContext());
//		DataSource dataSource = context.getBean("dataSource", DataSource.class);
//		String sql = "INSERT INTO t_login_log(userId, loginName, loginDate, ip, type) VALUES (?, ?, ?, ?, ?)";
//		SimpleJdbcTemplate jdbc = new SimpleJdbcTemplate(dataSource);
//		jdbc.update(sql, id, loginName, new java.util.Date(), getIpAddress(RequestLocal.getRequest()), 2);
	}
	
	public String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (StringUtils.isEmpty(ip) || ip.equalsIgnoreCase("unknown")) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (StringUtils.isEmpty(ip) || ip.equalsIgnoreCase("unknown")) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (StringUtils.isEmpty(ip) || ip.equalsIgnoreCase("unknown")) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
